import 'dart:io';

import 'package:get/get.dart';
import 'package:get/get_connect/http/src/request/request.dart';
import 'package:led_wall_simulator/screen_controller.dart';

class HttpService extends GetxService {
  HttpServer? httpServer;

  Future<HttpService> init() async {
    httpServer = await HttpServer.bind(
      InternetAddress.anyIPv4,
      8888,
      shared: true,
    );

    httpServer!.listen(
      (HttpRequest httpRequest) async {
        var body = await httpRequest.bytesToString();
        httpRequest.response.close();
        var controller = Get.find<ScreenController>();
        controller.setLeds(body);
      },
      onError: (e) {
        print(e);
      },
      onDone: () => print('done'),
    );

    return this;
  }
}
